package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import java.util.List;

import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.CarConst;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.adapter.CarManagerRecyclerAdapter;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActivityConst;
import cn.sirun.com.fca.personal.center.model.bean.CarListResponse;
import cn.sirun.com.fca.personal.center.presenter.ICarManagerPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.CarManagerPresenter;
import cn.sirun.com.fca.personal.center.view.ICarManagerView;
import cn.sirun.com.fca.personal.center.widget.RecyclerView;

public class CarManagerFlowView extends BaseLoadingFlowView<ICarManagerPresenter>
        implements ICarManagerView, CarManagerRecyclerAdapter.ViewClickListener {

    private RecyclerView mCarRV;

    private CarManagerRecyclerAdapter mAdapter;
    private List<CarInfoDetailResponse> mCarList;
    private String mCurDin;

    public CarManagerFlowView(Activity activity) {
        super(activity);
    }

    public CarManagerFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onGetCarListResult(BaseResponse<CarListResponse> response) {
        hideLoadingView();
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            handleCarListResult(response);
        }
    }

    @Override
    public void onSetCurCarResult(BaseResponse<Void> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            getPresenter().getCarList();
        } else {
            hideLoadingView();
        }
    }

    private void handleCarListResult(BaseResponse<CarListResponse> response) {
        mCarList = response.getData().getCarList();
        mCurDin = response.getData().getIovCurrentVehicle();
        TXSharedPreferencesUtils.setValue(CarConst.SpKey.CAR_ACTIVATE_INFO, mCurDin);
        if (mCarList != null && mCarList.size() > 0) {
            if (mAdapter == null) {
                mAdapter = new CarManagerRecyclerAdapter(getActivity());
                mAdapter.setViewClickListener(this);
                mAdapter.setmCarList(mCarList, mCurDin);
                mCarRV.setAdapter(mAdapter);
            } else {
                mAdapter.setmCarList(mCarList, mCurDin);
                mAdapter.notifyDataSetChanged();
            }
        } else {
            mCarRV.setVisibility(View.GONE);
        }
    }

    @Override
    protected ICarManagerPresenter createPresenter() {
        return new CarManagerPresenter();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        showLoadingView();
        getPresenter().getCarList();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(
                activity, ResourcesUtils.getString(R.string.car_manager),
                ResourcesUtils.getDrawable(R.color.white));
    }

    private void initView() {
        setContentView(R.layout.car_manager_activity);
        final Activity activity = getActivity();
        mCarRV = activity.findViewById(R.id.rv_car_list);
        mCarRV.setLayoutManager(new LinearLayoutManager(activity));
    }

    @Override
    public void carDetail(int position) {
        Intent intent = Router.fromPath(PersonalCenterActivityConst.Path.CAR_INFO);
        intent.putExtra(CarConst.SpKey.CAR_INFO_DETAIL, mCarList.get(position));
        ActivityUtils.startActivityForResult(getActivity(), intent);
    }

    @Override
    public void verifying(int position) {
        Intent intent = Router.fromPath(AccountActivityConst.Path.USER_ARGEEMENT);
        intent.putExtra("din", mCarList.get(position).getDin());
        ActivityUtils.startActivityForResult(getActivity(), intent);
    }

    @Override
    public void settingCurCar(int position) {
        String din = mCarList.get(position).getDin();
        showLoadingView();
        getPresenter().setCurrentVehicle(din);
    }
}
